#guiLogic.py - All of the logic unique to the console program

from fileSelection import *
from responseProcessing import *
from fileProcessing import *
import getpass
import time

class guiLogic(object):
    """Logic for gui."""
    
    def __init__(self):
        """Initializes."""
	self.initializeQuizDone=False
	self.endQuizDone=False
	self.fileProc=fileProcessing()
	self.drt='None'
        pass

    #The methods for the start screen

    def getFiles(self,fileTypes=['.csv'],directory=os.curdir):
        """Gets list of csv files for dropdown.

        Returns self.fileNames.
        self.fileNames is in the following format:
        self.fileNames[i] is a list of files of fileTypes[i] in
        directory.
        """
        self.myFolder=fileSelection(directory)
        self.fileNames=[]
        for fileType in fileTypes:
            self.fileNames.append(self.myFolder.createListByType(fileType))
        return self.fileNames
        
    def setWindowOneVariables(self,rosterFile,drt,numOfQuestions,user,password):
        """Sets the variables from window 1."""
        self.rosterFile=rosterFile
	self.drt=drt
        self.numOfQuestions=int(numOfQuestions)
        self.user=user
        self.password=password

    #The methods for the connecting screen

    def initializeQuiz(self):
        """Necessary before collection.

        Logs into gmail, sets the number of quiz questions, clears inbox.
        """
        self.responseProc=responseProcessing(self.numOfQuestions)
        self.responseProc.user(self.user)
        self.responseProc.pass_(self.password)
        self.responseProc.connect()
        self.responseProc.clearInbox()
        self.initializeQuizDone=True

    #The methods for the collection screen

    def collectionLoop(self):
        """Collects responses and updates strings.

        This method should be called to update the following strings:
        self.countRespones: String that describes response totals
        self.recentResponses: String that describes who has recently responded
        self.totalResponses: String that describes who has responded in total.
        No sleep built into this loop. 
        """
        self.responseProc.collectionLoop()
        self.countResponses = 'A total of ' + str(len(self.responseProc.responses)) + ' people have responded'
        
        self.recentResponsesPre = 'The following people just responded: '
        self.recentPeople=self.responseProc.peopleNew
        self.recentPeopleNum=[]
        for person in self.recentPeople:
            self.recentPeopleNum.append(int(person))
        self.recentResponses = self.recentResponsesPre + str(sorted(self.recentPeopleNum)).strip('[]')
        
        self.allPeopleNum=[]
        self.totalResponsesPre = 'The following people have responded: '
        for person in self.responseProc.responses:
            self.allPeopleNum.append(int(person))
        self.totalResponses = self.totalResponsesPre + str(sorted(self.allPeopleNum)).strip('[]')

    #The method to close the quiz and build the response file

    def endQuiz(self):
        """Disconnect and write response file."""
        self.responseProc.disconnect()
        self.fileProc.writeResultFile(self.rosterFile,self.drt,self.responseProc.responses,self.numOfQuestions)
        self.endQuizDone=True

    #The methods for the summary screen

    def responseSummary(self,numToDisplay=4):
        """Builds and returns response summary.

        String has newline characters \n built in
        for use with label widget.
        """
        self.summaryString=''
        self.responseProc.sortResponses()
        for qCount,question in enumerate(self.responseProc.sortedResponses):
            self.summaryString += 'For question ' + str(qCount+1) + ' the top answers were:\n'
            for ansCount,answerPair in enumerate(question):
                    if ansCount<=(numToDisplay-1):
                            self.summaryString += 'The answer ' + answerPair[0] + ' was given by ' + str(answerPair[1]) + ' people\n'
            self.summaryString += '\n'
        return self.summaryString
